package com.ticket.sass.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.common.entity.tenant.TenantCinemaMapEntity;
import com.ticket.sass.provider.mapper.TenantCinemaMapMapper;
import com.ticket.sass.provider.service.BaseService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ywb
 * @since 2024-03-18
 */
@Service
public class TenantCinemaMapServiceImpl extends ServiceImpl<TenantCinemaMapMapper, TenantCinemaMapEntity> implements BaseService<TenantCinemaMapEntity> {
    /**
     * 根据租户ID获取影院ID列表
     *
     * @param tenantId 租户ID
     * @return 影院ID列表
     */
    public List<Long> getCinemaIdsByTenantId(Long tenantId) {
        QueryWrapper<TenantCinemaMapEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("cinema_id").eq("tenant_id", tenantId);
        List<TenantCinemaMapEntity> tenantCinemaMapEntityList = baseMapper.selectList(queryWrapper);
        
        List<Long> cinemaIds = new ArrayList<>();
        for (TenantCinemaMapEntity entity : tenantCinemaMapEntityList) {
            cinemaIds.add(entity.getCinemaId());
        }
        return cinemaIds;
    }
    
    
    public Map<Long, Integer> getCinemaCountsByTenantIds(List<Long> tenantIds) {
        Map<Long, Integer> cinemaCountsMap = new HashMap<>();
        
        // 使用 QueryWrapper 构建查询条件
        QueryWrapper<TenantCinemaMapEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("tenant_id", tenantIds);
        
        // 查询符合条件的租户影院绑定列表
        List<TenantCinemaMapEntity> tenantCinemaMaps = baseMapper.selectList(queryWrapper);
        
        // 计算每个租户ID对应的影院数量，并放入 Map 中
        for (TenantCinemaMapEntity tenantCinemaMap : tenantCinemaMaps) {
            Long tenantId = tenantCinemaMap.getTenantId();
            Integer count = cinemaCountsMap.getOrDefault(tenantId, 0);
            cinemaCountsMap.put(tenantId, count + 1);
        }
        
        return cinemaCountsMap;
    }
    
    public Map<Long, List<TenantCinemaMapEntity>> getCinemasByTenantIds(List<Long> tenantIds) {
        Map<Long, List<TenantCinemaMapEntity>> cinemaCountsMap = new HashMap<>();
        
        // 使用 QueryWrapper 构建查询条件
        QueryWrapper<TenantCinemaMapEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("tenant_id", tenantIds);
        
        // 查询符合条件的租户影院绑定列表
        List<TenantCinemaMapEntity> tenantCinemaMaps = baseMapper.selectList(queryWrapper);
        
        // 将查询结果按租户ID分组放入 Map 中
        for (TenantCinemaMapEntity tenantCinemaMap : tenantCinemaMaps) {
            Long tenantId = tenantCinemaMap.getTenantId();
            List<TenantCinemaMapEntity> cinemaList = cinemaCountsMap.getOrDefault(tenantId, new ArrayList<>());
            cinemaList.add(tenantCinemaMap);
            cinemaCountsMap.put(tenantId, cinemaList);
        }
        
        return cinemaCountsMap;
    }
    
    public void removeBy(Long planId, List<Long> del) {
        if (del == null || del.size() == 0){
            return;
        }
        
        QueryWrapper<TenantCinemaMapEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(planId != null, "tenant_id", planId);
        queryWrapper.in("cinema_id", del);
        this.remove(queryWrapper);
    }
    
}
